<?php
namespace app\home\model;
use think\Model;

class OrdersModel extends Model
{
	protected $table  = 'qm_order';


	/**
	*
	*生成课程订单
	**/
	public function addMemberOrders($data)
	{
		$res = $this->insertGetId($data);
		return $res;
	}


	/**
	*
	*查找用户订单
	**/
	public function checkMemberOrders($order_number, $field = '*')
	{
		$info = $this->where('order_number', '=', $order_number)->field($field)->find();
		return $info;
	}


	/**
	*
	*更新用户订单
	**/
	public function updateOrders($where, $update)
	{
		$res = $this->where($where)->update($update);
		return $res;
	}


	/**
	*
	*用户订单列表
	**/
	public function memberOrderList($where, $page, $field = '*')
	{
		$keys = ($page - 1) * 10;
		$limit = 10;
		$list = $this->where($where)->field($field)->limit($keys, $limit)->order('add_time DESC')->select();
		if(!empty($list))
		{
			$list = $list->toArray();
		}
		
		return $list;
	}


	/**
	*
	*用户某一个订单
	**/
	public function memberOrderInfo($where, $field = '*')
	{
		$info = $this->where($where)->field($field)->find();
		return $info;
	}


	/**
	*
	*用户最近的一个成交订单
	**/
	public function getMemberOrderAddtimeAsc($member_id, $field = '*')
	{
		$info = $this->where('member_id', '=', $member_id)->where('status', 'in', [1,2,3,4,5])->field($field)->order('add_time DESC')->find();
		return $info;
	}



	/**
	*
	*
	*用户-已收货的订单金额
	**/
	public function getMemberOrderSuccess($member_id)
	{
		$money = $this->where('member_id', '=', $member_id)->where('status', '=', 4)->sum('pay_money');
		return $money;
	}




	/**
	*
	*查找未发货的订单，兑换码发货的
	*
	**/
	public function checkOrderStatusEqOneCourse()
	{
		$where = [
			['o.status', '=', 1],
			['c.attend_type', '=', 2],
		];
		$list = $this->alias('o')->where($where)->join('qm_course c','o.course_id = c.id')->order('o.add_time ASC')->limit(0, 500)->select();
		if(!empty($list)) $list = $list->toArray();
		return $list;
	}


	/**
	*
	*找出所有发货超十天的订单
	*
	**/
	public function orderAffirm()
	{
		$date = date('Y-m-d H:i:s', strtotime('-10 days'));
		$where = [
			['status', '=', 2],
			['dispatch_time', '<=', $date],
		];
		$list = $this->where($where)->order('add_time ASC')->select();
		if(!empty($list))
		{
			$list = $list->toArray();
		}
		
		return $list;
	}

}
